﻿<?php
session_start();
include "connectsql.php";
if (isset($_SESSION["ceo"]) && $_SESSION["ceo"] == true) {
	if (isset($_REQUEST["state"])) {
		$state = $_REQUEST["state"];
		if ($state == "")
			$state = 0;
	} else {
		$state = 0;
	}
		
	$manager = $_SESSION["manager"];
	$sql = "select * from ".DB_MANAGER_TAB." where `managername` = '".$manager."'";
	$query = mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
	$num_rows = mysqli_num_rows($query);

	if ($num_rows != 0) {
		$row = mysqli_fetch_assoc($query);
		mysqli_free_result($query);		
		$leaderid = $row["leaderid"];
		if ($leaderid != 0) {
			$sql = "select * from ".DB_MANAGER_TAB." where `id` = '".$leaderid."'";
			$query = mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
			if ($num_rows != 0) {
				$row = mysqli_fetch_assoc($query);
				mysqli_free_result($query);
				$manager = $row["managername"];	
			} else {
				die (ECHO_ERROR);
			}
		}
	} else {
		die (ECHO_ERROR);
	}
	
	$error = "";
	$showtxt = "machineliststart:";	
	$sql = "select * from ".SetDB_USER_TAB($manager)." order by `username`";	
	if ($query = mysqli_query($link, $sql)) {
		$num_rows = mysqli_num_rows($query);
		if ($num_rows != 0) {
			$num = 0;
			while ($row = mysqli_fetch_assoc($query)) {          
				$username = $row["username"];
				$alias = $row["alias"];
				$groupname = $row["groupname"];
				$remarks = $row["remarks"];
				$gprs = $row["extrainfo"];
				$cardnum = $row["cardnum"];
				$modexml = $row["modexml"];
				$playlistxml = $row["playlistxml"];
				$login_time = $row["login_time"];
				$ver = $row["ver"];
				$downloadper = $row["downloadper"];
				$gps = $row["gps"];		
				$mcu = $row["mcu"];
				$network = $row["network"];
				$expand = $row["expand"];
				$error = $row["error"];
				$gprslimit = $row["gprslimit"];
				$commaloc = strpos($gprslimit, ",");
				if ($commaloc == false) {
					$gprslimit = "0";
				} else {
					$gprslimit = substr($gprslimit, 0, $commaloc);
				}				
				if ($login_time == 0) {
					$login_time = "";
				} else {
					$login_time = date('Y-m-d H:i:s', $login_time);			
				}
				if ($num != 0)
					$showtxt .= ";;";
				$showtxt .= $username.",,".$alias.",,".$groupname.",,".$login_time.",,".$remarks.",,".$cardnum.",,".$modexml.",,".$playlistxml.",,".$ver.",,".$gprs;
				$showtxt .= ",,".$downloadper.",,".$mcu.",,".$gps.",,".$expand.",,".$error.",,".$network.",,".$gprslimit;
				$num++;
			}
		}
		$showtxt .= ":machinelistover";	
		date_default_timezone_set('PRC');
		echo $showtxt.date('Y-m-d H:i:s');
		mysqli_free_result($query);
	} else {
		die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
	}
}
mysqli_close($link);
?>